import { _decorator, Component, Node, v3 } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('StarSky')
export class StarSky extends Component {

    private redBg: Node

    private greenBg: Node

    private curBg: Node

    private speed: number

    onLoad(): void {
        this.redBg = this.node.getChildByName("RedBg")
        this.greenBg = this.node.getChildByName("GreenBg")
        this.curBg = this.redBg;
        this.speed = -100
        this.node.setSiblingIndex(-2000)
    }

    update(deltaTime: number) {
        let dealtY = deltaTime * this.speed
        let redPos = this.redBg.getPosition()
        let redPosNew = redPos.add3f(0, dealtY, 0);
        this.redBg.setPosition(redPosNew)

        let greenPos = this.greenBg.getPosition()
        let greenPosNew = greenPos.add3f(0, dealtY, 0);
        this.greenBg.setPosition(greenPosNew)

        if (this.curBg.getPosition().y <= -1360) {
            if (this.curBg == this.redBg) {
                this.redBg.setPosition(greenPosNew.x, greenPosNew.y + 1369, 0)
                this.curBg = this.greenBg;
            } else {
                this.greenBg.setPosition(redPos.x, redPos.y + 1369, 0)
                this.curBg = this.redBg;
            }
        }

    }
}

